// 在定义接口时, 为接口中的属性或方法定义泛型类型
// 在使用接口时, 再指定具体的泛型类型
(()=>{

    interface IBaseCRUD<T>{
        id:Array<T>
        add:(t:T) => T
        getUserId:(id:number) => T

    }

    class User {
        id?:number    //?表示可有可无
        name:string
        age:number
        constructor( name:string,age:number){
            this.name = name
            this.age = age
        }
    }

    class  UserCRUD implements IBaseCRUD<User>{
        date:Array<User> = []
        add(user:User):User{
            user.id = Date.now()+Math.random()
            this.date.push(user)
            return user
        }

        getUserId(id:number): User{
          return this.date.find(user => user.id === id)
        }
    }

    const userCRUD:UserCRUD = new UserCRUD()
    userCRUD.add(new User('jack',20))
    userCRUD.add(new User('tom',21))
    userCRUD.add(new User('lucy',22))
    const {id} = userCRUD.add(new User('rousi',23))
    console.log(userCRUD.date)
    console.log(userCRUD.getUserId(id))

})()